package com.bdzl.admin.controller.data;

import com.bdzl.admin.service.data.UserInfoService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.data.UserInfoDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.data.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -用户身份基础信息表，包括客户与系统用的基础信息
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:57:43
 */

@Controller
@RequestMapping("/bdzl/userInfo")
public class UserInfoController extends BaseController {
	@Autowired
	private UserInfoService userInfoService;

	@GetMapping()
	String UserInfo(){
	    return "bdzl/userInfo/userInfo";
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 只查询关联订单的客户
	 * @Date 10:26 2021/5/22
	 * @Param [params]
	 * @return com.bdzl.pub.util.param.PageUtils
	 **/
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserInfoDO> userInfoList = userInfoService.customerList(query);
		int total = userInfoService.customerListCount(query);
		PageUtils pageUtils = new PageUtils(userInfoList, total);
		return pageUtils;
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 查询所有个人信息
	 * @Date 10:26 2021/5/22
	 * @Param [params]
	 * @return com.bdzl.pub.util.param.PageUtils
	 **/
	@ResponseBody
	@GetMapping("/listAll")
	public PageUtils listAll(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<UserInfoVO> userInfoList = userInfoService.list(query);
		int total = userInfoService.count(query);
		PageUtils pageUtils = new PageUtils(userInfoList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/userInfo/add";
	}

	@GetMapping("/edit/{conId}")
	@ResponseBody
	public UserInfoDO edit(@PathVariable("conId") Integer conId){
	    return userInfoService.get(conId);
	}

	/**
	* @Description: TODO 获取客户数据
	* @param conId 客户id
	* @param odrNo 订单号
	* @return: com.bdzl.pub.vo.data.UserInfoVO
	* @Author: Ming
	* @Date: 2021/5/18 19:24
	*/
	@PostMapping("/getUserInfo")
	@ResponseBody
	public UserInfoVO getUserInfo(@RequestParam("conId")Integer conId,@RequestParam("odrNo")String odrNo){
		UserInfoDO userInfoDO = userInfoService.get(conId);
		UserInfoVO userInfoVO = new UserInfoVO();
		//类拷贝
		BeanUtils.copyProperties(userInfoDO, userInfoVO);

		if (userInfoDO.getConSpouse()!=null){
			//查询配偶信息
			UserInfoDO userInfoSpouse = userInfoService.getSpouse(userInfoDO.getConSpouse());
			if (userInfoSpouse != null){
				UserInfoVO userInfoVO1 = new UserInfoVO();
				//类拷贝
				BeanUtils.copyProperties(userInfoSpouse, userInfoVO1);
				userInfoVO.setSpouse(userInfoVO1);
			}
		}
		if ("-1".equals(odrNo) || odrNo != null ){
			//查询担保人信息
			List<UserInfoVO> guarantor = userInfoService.getGuarantor(conId, odrNo);
			userInfoVO.setGuarantorList(guarantor);
		}
		return userInfoVO;
	}

	/**
	 * @Description: TODO 根据身份证号查询数据
	 * @param conCardid 身份证号
	 * @return: com.bdzl.pub.entity.data.UserInfoDO
	 * @Author: Ming
	 * @Date: 2021/5/20 20:04
	 */
	@PostMapping("/getSpouse")
	@ResponseBody
	public UserInfoDO getSpouse(@RequestParam("conCardid") String conCardid){
		return userInfoService.getSpouse(conCardid);
	}
	/**
	 * @Description: TODO 获取客户数据
	 * @param conId 客户id
	 * @return: com.bdzl.pub.vo.data.UserInfoVO
	 * @Author: Ming
	 * @Date: 2021/5/18 19:24
	 */
	@GetMapping("/getUserInfoData")
	@ResponseBody
	public UserInfoVO getUserInfo(Integer conId){
		UserInfoDO userInfoDO = userInfoService.get(conId);
		UserInfoVO userInfoVO = new UserInfoVO();
		//类拷贝
		BeanUtils.copyProperties(userInfoDO, userInfoVO);
		Map<String,Object> map = new HashMap<>();
		map.put("conCardid",userInfoDO.getConSpouse());
		//查询配偶信息
		List<UserInfoVO> userInfoDOList = userInfoService.list(map);
		if (userInfoDOList != null && userInfoDOList.size()>0){
			userInfoVO.setSpouse(userInfoDOList.get(0));
		}
		return userInfoVO;
	}

	/**
	 * @Description: TODO 获取客户数据
	 * @param conId 客户id
	 * @return: com.bdzl.pub.vo.data.UserInfoVO
	 * @Author: Ming
	 * @Date: 2021/5/18 19:24
	 */
	@PostMapping("/getOneUserInfo")
	@ResponseBody
	public UserInfoVO getOneUserInfo(@RequestBody UserInfoDO conId){
		UserInfoDO userInfoDO = userInfoService.get(conId.getConId());
		UserInfoVO userInfoVO = new UserInfoVO();
		//类拷贝
		BeanUtils.copyProperties(userInfoDO, userInfoVO);
		return userInfoVO;
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping(value = "/save")
	public R save(@RequestBody UserInfoDO userInfo){
		if(userInfoService.save(userInfo)>0){
			return R.ok(userInfo.getConId());
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody UserInfoDO userInfo){
		userInfoService.update(userInfo);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(@RequestParam("conId") Integer conId){
		if(userInfoService.remove(conId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] conIds){
		userInfoService.batchRemove(conIds);
		return R.ok();
	}

	/**
	* @Description: TODO 获取所有
	* @param params
	* @return: java.util.List<com.bdzl.pub.entity.data.UserInfoVO>
	* @Author: Ming
	* @Date: 2021/5/25 18:01
	*/
	@ResponseBody
	@PostMapping("/userInfoList")
	public List<UserInfoVO> getUserInfoList(@RequestBody Map<String,Object> params){
		//查询列表数据
		List<UserInfoVO> userInfoList = userInfoService.list(params);
		return userInfoList;
	}

	/**
	 * @Description: TODO 根据角色获取用户
	 * @param params
	 * @return: java.util.List<com.bdzl.pub.entity.data.UserInfoVO>
	 * @Author: Ming
	 * @Date: 2021/5/25 18:01
	 */
	@ResponseBody
	@GetMapping("/getUserInfoByRele/{roleId}")
	public List<UserInfoDO>getUserInfoByRele(@PathVariable("roleId") Integer roleId){
		return  userInfoService.getUserInfoByRele(roleId);
	}
}
